package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.bean.Room;
import com.hotel.dao.AccountDao;
import com.hotel.dao.RoomDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author KungSu.
 * @create 2021-05-10 15:43
 */
@WebServlet(value = "/servlet")
public class TestServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String type = request.getParameter("type");
        System.out.println(type);
        switch (type) {
            case "register":
                //获取表单中的信息
                String user = request.getParameter("user");
                String phoneNo = request.getParameter("phoneNo");
                String pwd = request.getParameter("pwd");
                String Email = request.getParameter("Email");

                //获取sqlSession用来使用数据库
                SqlSession sqlSession = DBOperation.getSqlSession();
                //通过sqlSession创建AccountDao接口的实现类
                AccountDao accountDao = sqlSession.getMapper(AccountDao.class);

                //创建一个Account实体类
                Account account = new Account();
                account.setPassword(pwd);
                account.setUsername(user);
                account.setPhone(phoneNo);
                account.setEmail(Email);
                account.setCreate_day(new Date());

                //测试：查看新建的实体类
                System.out.println(account);
                System.out.println("保存操作之前:" + account.getAccount_id());

                //本次与数据库交互的操作
                accountDao.saveUser(account);

                //保存后的测试
                System.out.println("保存操作之后：" + account.getAccount_id());
                sqlSession.close();
                response.sendRedirect("jsp/login.jsp");

                break;
            case "login":
                String username = request.getParameter("user");
                String password = request.getParameter("password");
                SqlSession sqlSession1 = DBOperation.getSqlSession();
                AccountDao accountDao1 = sqlSession1.getMapper(AccountDao.class);


            default:
                response.sendRedirect("jsp/register.jsp");

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
